package com.yanghongyue.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yanghongyue.reggie.bean.AddressBook;
import com.yanghongyue.reggie.common.BaseContext;
import com.yanghongyue.reggie.common.R;
import com.yanghongyue.reggie.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author HongyueYang
 */
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    //查询当前用户的所有收货地址
    @GetMapping("/list")
    public R<List> list() {
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.orderByDesc("is_default");
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return R.sucess(list);
    }

    //获取信息
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id) {
        AddressBook addressBook = addressBookService.getById(id);
        return R.sucess(addressBook);
    }

//     //添加地址
//    @PostMapping
//    public R<String> save(@RequestBody AddressBook addressBook) {
//        //addressBook.setUserId(BaseContext.getCurrentId());
//        addressBookService.save(addressBook);
//        return R.sucess("添加成功");
//    }


    //添加收货地址
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook) {
        //新增地址之前 查询一下 该用户是否有地址
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", BaseContext.getCurrentId());
        List<AddressBook> addressBooks = addressBookService.list(wrapper);
        if (addressBooks == null || addressBooks.size() <= 0) {
            //如果当前用户没有默认地址 设置当前地址是默认的
            addressBook.setIsDefault(1);
        }
        //设置用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.sucess("添加收货地址成功");
    }


    //修改地址
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return R.sucess("修改成功");
    }

    //删除地址
    @DeleteMapping
    public R<String> delete(Long[] ids) {
        addressBookService.removeByIds(Arrays.asList(ids));
        return R.sucess("删除成功");

    }

    //设置默认地址
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook) {
        //将当前用户的所有地址的is_default都设置为0
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        AddressBook addressBook1 = new AddressBook();
        addressBook1.setIsDefault(0);
        addressBookService.update(addressBook1, queryWrapper);
        //将当前地址的is_default设置为1
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.sucess("设置成功");
    }

    //查询默认地址
    @GetMapping("/default")
    public R<AddressBook> getDefault() {
        QueryWrapper<AddressBook> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", BaseContext.getCurrentId());
        queryWrapper.eq("is_default", 1);
        AddressBook addressBook = addressBookService.getOne(queryWrapper);
        if (addressBook == null) {
            return R.error("没有找到该对象");
        } else {
            return R.sucess(addressBook);
        }
    }


}

